const net = require('net')

const server = net.createServer((socket) => {
  // socket is a TCP connection
  console.log('client connected')
  socket.on('data', (data) => {
    console.log(`Received: ${data}`)
    socket.write(`Hello, ${data.toString()}`) // send back data as response
  })
  socket.on('close', () => {
    console.log('client disconnected')
  })
})

server.listen(8080, () => {
  console.log('server listening on port 8080')
})
